package com.eascs.dubbo.api.creditaudit;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.workflow.CreditAuditApiService;
import com.eascs.admin.api.workflow.vo.CreditAuditSubmitVo;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oLoadUserZxServiceImpl;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.zx.entity.O2oLoanUserEntityZx;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.admin.o2o.zx.vo.O2oZxVo;
import com.eascs.common.util.StringUtil;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.file.FileTypeConstants;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Service
public class CreditAuditServiceImpl implements CreditAuditApiService {

	@Reference
	SysOrgService sysOrgService;
	
	@Autowired
	private O2oZxServiceImpl o2oZxServiceImpl;
	
	@Autowired
	private O2oDataListService o2oDataListService;
	
	@Autowired
	private O2oLoadUserZxServiceImpl o2oLoadUserZxServiceImpl;
	
	@Override
	public String zbCreditAuditSubmit(CreditAuditSubmitVo creditAuditSubmitVo) {
		O2oZxEntity o2oZxEntity = o2oZxServiceImpl.getEntityById(creditAuditSubmitVo.getReferId());

		if (creditAuditSubmitVo.getZxDate() == null) {
			return "请填客户征信报告时间";
		}

		List<O2oDataListEntity> dataList = o2oDataListService.findByEntityList(creditAuditSubmitVo.getReferId());
		O2oDataListEntity ZXBGDATAEntity = null;
		for (int i = 0; i < dataList.size(); i++) {
			O2oDataListEntity entity = dataList.get(i);
			if (entity.getDatalistCode().equals(FileTypeConstants.DALIST_TYPE_ZXBGDATA)) {
				ZXBGDATAEntity = entity;
			}
		}
		if (ZXBGDATAEntity == null) {
			return "请先上传客户征信报告后再进行操作";
		}

		O2oLoanUserEntityZx o2oLoanUserEntityZx = o2oLoadUserZxServiceImpl.getEntityByZxNo(creditAuditSubmitVo.getReferId());
		if (creditAuditSubmitVo.getZxDate() != null) {
			o2oLoanUserEntityZx.setZxDate(creditAuditSubmitVo.getZxDate());
		}
		if (creditAuditSubmitVo.getZxTdDate() != null) {
			o2oLoanUserEntityZx.setZxTdDate(creditAuditSubmitVo.getZxTdDate());
		}
		o2oZxServiceImpl.update(o2oZxEntity);

		o2oLoadUserZxServiceImpl.update(o2oLoanUserEntityZx);

		return null; 
	}

	@Override
	public String bCCreditReviewSubmit(O2oZxVo o2oZxVo) {
		O2oZxEntity o2oZxEntity = null;
		if (o2oZxVo.getId() != null) {
			o2oZxEntity = o2oZxServiceImpl.getEntityById(o2oZxVo.getId());
		} else {
			return "未找到该征信单的id";
		}

		if (o2oZxEntity == null) {
			return "系统中未找到该征信单的信息，请联系管理员";
		}

		if (o2oZxVo.getPurchaseAmount() == null) {
			return "企业采购金额不能为空";
		}
		
		if (StringUtil.isBlank(o2oZxVo.getJtdName())) {
			return "经办平台确认不能为空";
		} else if (o2oZxVo.getJtdName().contains("金融部")) {// 如果经办平台确认字段名称中包含“金融部”三个字
			return "经办平台不能是省区金融部";
		}

		ActOrgEntity actOrgEntity = null;
		actOrgEntity = sysOrgService.getOrgByUnitNo(o2oZxVo.getJtd());
		o2oZxEntity.setPurchaseAmount(o2oZxVo.getPurchaseAmount());
		o2oZxEntity.setJtd(o2oZxVo.getJtd());
		o2oZxEntity.setJtdCode(actOrgEntity.getUnitCode());
		o2oZxEntity.setJtdName(actOrgEntity.getOrgName());

		User user = UserHolder.getUser();
		o2oZxEntity.setSubmitDate(new Date());
		o2oZxEntity.setSubmitId(user.getNo());
		o2oZxEntity.setSubmitName(user.getCname());

		int i = o2oZxServiceImpl.update(o2oZxEntity);
		if (i > 0) {
			return null;
		} else {
			return "保存失败";
		}
	}
}
